﻿Public Class RCMS_CBLVSorter
    Implements IComparer
    Private col As Integer
    Private order As System.Windows.Forms.SortOrder = Windows.Forms.SortOrder.Ascending

    Public Sub New()
        col = 0
        order = Windows.Forms.SortOrder.Ascending
    End Sub

    Public Sub New(ByVal column As Integer, ByVal order As System.Windows.Forms.SortOrder)
        Me.col = column
        Me.order = order
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                            Implements System.Collections.IComparer.Compare
        Dim returnVal As Integer = -1
        Dim s1 As String = CType(x, System.Windows.Forms.ListViewItem).SubItems(col).Text
        Dim s2 As String = CType(y, System.Windows.Forms.ListViewItem).SubItems(col).Text
        If col = 1 Then
            Dim i1 As Integer = InStrRev(s1, "-", -1, CompareMethod.Text)
            Dim i2 As Integer = InStrRev(s2, "-", -1, CompareMethod.Text)
            If i1 > 0 And i2 > 0 Then
                s1 = Mid(s1, 1, i1) & Mid(s1, i1 + 1).PadLeft(4, "0")
                s2 = Mid(s2, 1, i2) & Mid(s2, i2 + 1).PadLeft(4, "0")
            End If
        End If
        If col <> 2 Then
            Dim s3 As String = CType(x, System.Windows.Forms.ListViewItem).SubItems(2).Text
            Dim s4 As String = CType(y, System.Windows.Forms.ListViewItem).SubItems(2).Text
            If s3 = "-" Then
                If order = Windows.Forms.SortOrder.Descending Then
                    s3 = "AAAA"
                Else
                    s3 = "ZZZZ"
                End If
            End If
            If s4 = "-" Then
                If order = Windows.Forms.SortOrder.Descending Then
                    s4 = "AAAA"
                Else
                    s4 = "ZZZZ"
                End If
            End If
            s1 = s3 & s1
            s2 = s4 & s2
        End If
        returnVal = [String].Compare(s1, s2)
        If order = Windows.Forms.SortOrder.Descending Then
            returnVal *= -1
        End If
        Return returnVal
    End Function
End Class


